"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClubUserDao = void 0;
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const DateUtil_1 = require("../../src/engine/utils/DateUtil");
const DBSourceManager_1 = require("../../src/engine/utils/DBSourceManager");
const ClubUserEntity_1 = require("../entity/ClubUserEntity");
const ClubConfig_1 = require("../config/ClubConfig");
class ClubUserDao extends DaoBase_1.DaoBase {
    static getRepository() {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepositoryBase(ClubUserEntity_1.ClubUserEntity, this.getGameDbName());
            return repository;
        });
    }
    static get_club_user_by_id(id) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result = yield repository.findOneBy({
                id: id,
            });
            return result;
        });
    }
    static get_club_user(clubid, userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result = yield repository.findOneBy({
                clubid: clubid,
                userid: userid
            });
            return result;
        });
    }
    static add_club_user(clubid, userid, actor, user_state) {
        return __awaiter(this, void 0, void 0, function* () {
            let entity = new ClubUserEntity_1.ClubUserEntity();
            entity.clubid = clubid;
            entity.userid = userid;
            entity.user_state = user_state;
            entity.actor = actor;
            entity.join_time = DateUtil_1.default.formatDateStr(DateUtil_1.default.now());
            let repository = yield this.getRepository();
            let result = yield DBSourceManager_1.DBSourceManager.instance.insert(repository, entity);
            if (result && result.raw["affectedRows"] > 0) {
                return true;
            }
            return false;
        });
    }
    static update_user(entity) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result;
            if (!repository.hasId(entity)) {
                result = yield repository.save(entity);
            }
            else {
                let updateResult = yield repository.update({ id: entity.id }, entity);
                if (updateResult && updateResult.affected > 0) {
                    result = entity;
                }
            }
            return result;
        });
    }
    static getUserCountByClub(clubid) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let count = yield repository.countBy({
                clubid: clubid,
                user_state: ClubConfig_1.ClubConfig.Club_merber_state_type.nomal
            });
            return count;
        });
    }
    static req_club_apply_list(clubid) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "select * from " + this.Tb_Name + ",t_users where `t_clubusers`.`clubid` = ? and `t_clubusers`.`user_state`=0 and `t_clubusers`.`userid`=`t_users`.`userid`";
            let sqlParams = [clubid];
            let repository = yield this.getRepository();
            let results = yield repository.query(sql, sqlParams);
            let users = this.obj2IClubUserList(results);
            return users;
        });
    }
    static club_apply_handle(id, isAgree, apply_userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "update t_clubusers set `user_state`=?, `approver`=?  where `id`=?";
            let user_state = ClubConfig_1.ClubConfig.Club_merber_state_type.nomal;
            if (!isAgree) {
                user_state = ClubConfig_1.ClubConfig.Club_merber_state_type.refuse;
            }
            let sqlParams = [user_state, apply_userid, id];
            let repository = yield this.getRepository();
            let results = yield repository.query(sql, sqlParams);
            // Logger.log("club_apply_handle==", results)
            if (results.affectedRows > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static obj2IClubUserList(results) {
        let users = [];
        for (let i = 0; i < results.length; i++) {
            let user = {
                id: results[i]["id"],
                clubid: results[i]["clubid"],
                userid: results[i]["userid"],
                userState: results[i]["user_state"],
                actor: results[i]["actor"],
                joinTime: new Date(results[i]["join_time"]).getTime(),
                approver: results[i]["approver"],
                score: results[i]["score"],
                headimgurl: results[i]["headimgurl"],
                nickName: results[i]["nick_name"],
            };
            users[i] = user;
        }
        return users;
    }
    static req_club_member_list(clubid) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "select * from " + this.Tb_Name + ",t_users where `t_clubusers`.`clubid` = ? and `t_clubusers`.`user_state`=? and `t_clubusers`.`userid`=`t_users`.`userid`";
            let sqlParams = [clubid, ClubConfig_1.ClubConfig.Club_merber_state_type.nomal];
            let repository = yield this.getRepository();
            let results = yield repository.query(sql, sqlParams);
            let users = this.obj2IClubUserList(results);
            return users;
        });
    }
}
exports.ClubUserDao = ClubUserDao;
ClubUserDao.Tb_Name = "t_clubusers";
